package com.example.detectivedemo.infrastructure.product;

import com.example.detectivedemo.domain.product.Product;
import com.example.detectivedemo.domain.product.ProductRepository;
import com.example.detectivedemo.infrastructure.generic.GenericDao;
import com.example.detectivedemo.utils.LogUtils;

import ohos.data.rdb.ValuesBucket;
import ohos.data.resultset.ResultSet;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * 产品数据访问对象
 * 负责产品数据的CRUD操作和数据库交互
 */
public class ProductDao extends GenericDao<Product> implements ProductRepository {
    
    private static final String TABLE_NAME = "products";
    private static final String COLUMN_NAME = "name";
    private static final String COLUMN_DESCRIPTION = "description";
    private static final String COLUMN_PRICE = "price";
    private static final String COLUMN_STOCK = "stock";
    private static final String COLUMN_CATEGORY = "category";
    
    public ProductDao() {
        super(TABLE_NAME);
        LogUtils.info("ProductDao initialized");
    }
    
    /**
     * 创建产品表
     * @return 创建表的SQL语句
     */
    @Override
    protected String createTable() {
        return "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (" +
                COLUMN_ID + " TEXT PRIMARY KEY, " +
                COLUMN_NAME + " TEXT NOT NULL, " +
                COLUMN_DESCRIPTION + " TEXT, " +
                COLUMN_PRICE + " DOUBLE NOT NULL, " +
                COLUMN_STOCK + " INTEGER NOT NULL, " +
                COLUMN_CATEGORY + " TEXT, " +
                COLUMN_CREATE_TIME + " TEXT NOT NULL, " +
                COLUMN_UPDATE_TIME + " TEXT NOT NULL, " +
                COLUMN_DELETED + " INTEGER NOT NULL DEFAULT 0" +
                ")";
    }
    
    /**
     * 将产品实体转换为ValuesBucket
     * @param product 产品实体
     * @return ValuesBucket对象
     */
    @Override
    protected ValuesBucket entityToValuesBucket(Product product) {
        ValuesBucket values = new ValuesBucket();
        values.putString(COLUMN_ID, product.getId());
        values.putString(COLUMN_NAME, product.getName());
        values.putString(COLUMN_DESCRIPTION, product.getDescription());
        values.putDouble(COLUMN_PRICE, product.getPrice());
        values.putInteger(COLUMN_STOCK, product.getStock());
        values.putString(COLUMN_CATEGORY, product.getCategory());
        values.putString(COLUMN_CREATE_TIME, product.getCreateTime().toString());
        values.putString(COLUMN_UPDATE_TIME, product.getUpdateTime().toString());
        values.putInteger(COLUMN_DELETED, product.isDeleted() ? 1 : 0);
        return values;
    }
    
    /**
     * 将ResultSet转换为产品实体
     * @param resultSet 查询结果集
     * @return 产品实体
     */
    @Override
    protected Product resultSetToEntity(ResultSet resultSet) {
        Product product = new Product();
        
        product.setId(resultSet.getString(COLUMN_ID));
        product.setName(resultSet.getString(COLUMN_NAME));
        product.setDescription(resultSet.getString(COLUMN_DESCRIPTION));
        product.setPrice(resultSet.getDouble(COLUMN_PRICE));
        product.setStock(resultSet.getInt(COLUMN_STOCK));
        product.setCategory(resultSet.getString(COLUMN_CATEGORY));
        product.setCreateTime(parseLocalDateTime(resultSet.getString(COLUMN_CREATE_TIME)));
        product.setUpdateTime(parseLocalDateTime(resultSet.getString(COLUMN_UPDATE_TIME)));
        product.setDeleted(resultSet.getInt(COLUMN_DELETED) == 1);
        
        return product;
    }
    
    /**
     * 根据分类查询产品
     * @param category 产品分类
     * @return 分类下的所有产品
     */
    @Override
    public List<Product> findByCategory(String category) {
        LogUtils.info("Finding products by category: " + category);
        String sql = "SELECT * FROM " + TABLE_NAME + " WHERE " + COLUMN_CATEGORY + " = ? AND " + COLUMN_DELETED + " = 0";
        return executeQuery(sql, new String[]{category});
    }
    
    /**
     * 根据价格范围查询产品
     * @param minPrice 最低价格
     * @param maxPrice 最高价格
     * @return 价格范围内的所有产品
     */
    @Override
    public List<Product> findByPriceRange(double minPrice, double maxPrice) {
        LogUtils.info("Finding products by price range: " + minPrice + "-" + maxPrice);
        String sql = "SELECT * FROM " + TABLE_NAME + " WHERE " + COLUMN_PRICE + " BETWEEN ? AND ? AND " + COLUMN_DELETED + " = 0";
        return executeQuery(sql, new String[]{String.valueOf(minPrice), String.valueOf(maxPrice)});
    }
    
    /**
     * 根据名称模糊查询产品
     * @param name 产品名称关键词
     * @return 匹配的产品列表
     */
    @Override
    public List<Product> findByNameLike(String name) {
        LogUtils.info("Finding products by name like: " + name);
        String sql = "SELECT * FROM " + TABLE_NAME + " WHERE " + COLUMN_NAME + " LIKE ? AND " + COLUMN_DELETED + " = 0";
        return executeQuery(sql, new String[]{"%" + name + "%"});
    }
    
    /**
     * 模拟从数据库查询产品数据
     * @return 产品列表
     */
    public List<Product> getAllProducts() {
        LogUtils.info("Getting all products");
        return findAll();
    }
    
    /**
     * 模拟添加产品数据
     * @param product 产品对象
     * @return 添加成功的产品
     */
    public Product addProduct(Product product) {
        LogUtils.info("Adding product: " + product.getName());
        return save(product);
    }
    
    /**
     * 模拟更新产品数据
     * @param product 产品对象
     * @return 更新成功的产品
     */
    public Product updateProduct(Product product) {
        LogUtils.info("Updating product: " + product.getId());
        return update(product);
    }
    
    /**
     * 模拟删除产品数据
     * @param id 产品ID
     * @return 删除是否成功
     */
    public boolean deleteProduct(String id) {
        LogUtils.info("Deleting product: " + id);
        return deleteById(id);
    }
    
    /**
     * 模拟根据ID查询产品
     * @param id 产品ID
     * @return 产品对象
     */
    public Optional<Product> getProductById(String id) {
        LogUtils.info("Getting product by id: " + id);
        return findById(id);
    }
}